/**
 * @file ogas.c
 * @brief 此文件包含了人机交互界面的关键代码。
 */

#include "zf_common_headfile.h"

OGAS_state state_ac = OGAS_MODE;
int ogas_ready = 0; // 此变量为1将结束OGAS主循环以准备发车。
OGAS_PAGE page_now = OGAS_MASTER; // OGAS主循环需要显示的页面。
int page_target = 0; // 各页面的选择目标。
OGAS_PAGE page_last; // 上一个加载的页面。
uint8 symbol = 0;    // flash界面状态标志

/**
 * @brief 获取各按键状态。
 *
 * @param wait 是否循环等待直到按键按下
 * @return 按键状态
 */
OGAS_KEY ogas_get_key(int wait) {
    do {
        if (key_get_state(KEY_1)) { return key_get_state(KEY_1) + 0; }
        if (key_get_state(KEY_2)) { return key_get_state(KEY_2) + 2; }
        if (key_get_state(KEY_3)) { return key_get_state(KEY_3) + 4; }
        if (key_get_state(KEY_4)) { return key_get_state(KEY_4) + 6; }
    } while (wait);
    return EMPTY;
}

/**
 * @brief 通过人机交互界面修改一个整型变量的值
 *
 * @param variable 变量地址
 * @param name 需要显示的字符串（不超过16字符）
 */
void ogas_change_variable_int(int* variable, char* name) {
    tft180_clear();
    tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
    tft180_show_string(column(0), line(1), "<CHG-VAR>");
    tft180_show_string(column(0), line(3), name);
    tft180_show_string(column(0), line(9), "[ - / + /COF/CC]");
    int original_value = *variable;
    
    while (1) {
        tft180_show_int(column(0), line(4), *variable, 10);
        
        switch (ogas_get_key(1)) {
            case K1_S:
            case K1_L:
                *variable-=1;
                break;
            case K2_S:
            case K2_L:
                *variable+=1;
                break;
            case K3_S: // 结束更改
                return;
            case K4_S: // 放弃更改
                *variable = original_value;
                return;
            default:break;
        }
    }
}

/**
 * @brief 通过人机交互界面修改一个浮点变量的值
 *
 * @param variable 变量地址
 * @param name 需要显示的字符串（不超过16字符）
 */
void ogas_change_variable_float(float* variable, char* name) {
    tft180_clear();
    tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
    tft180_show_string(column(0), line(1), "<CHG-VAR>");
    tft180_show_string(column(0), line(3), name);
    tft180_show_string(column(0), line(9), "[ - / + /COF/CC]");
    float original_value = *variable;
    
    while (1) {
        tft180_show_float(column(0), line(4), *variable, 5, 5);
        
        switch (ogas_get_key(1)) {
            case K1_S:
            case K1_L:
                *variable -= 0.1;
                break;
            case K2_S:
            case K2_L:
                *variable += 0.1;
                break;
            case K3_S: // 结束更改
                return;
            case K4_S: // 放弃更改
                *variable = original_value;
                return;
            default:break;
        }
    }
}

/*显示模板
tft180_show_string(0,line(0), "408 Lab. cWUI-V2");
tft180_show_string(0,line(1), "<MAETSR>");
tft180_show_string(0,line(2), "");
tft180_show_string(0,line(3), "");
tft180_show_string(0,line(4), "");
tft180_show_string(0,line(5), "");
tft180_show_string(0,line(6), "");
tft180_show_string(0,line(7), "");
tft180_show_string(0,line(8), "");
tft180_show_string(0,line(9), "");
}*/


/**
 * 
 * @brief 人机交互界面的主循环程序。              -致瞬息万变之物，及亘古不变之物-
 * 
 */
void OGAS(void) {
    //ogas_welcome();
    while (!ogas_ready) {
        tft180_clear();

        if (page_now != page_last) {
            page_target = 0;
        }
        page_last = page_now;

        switch (page_now) {
            case OGAS_MASTER:
                ogas_page_master();
                break;
            case OGAS_START:
                ogas_page_start();
                break;
            case EXEQ:
                ogas_page_exeq();
                break;
            case OGAS_CAMREA:
                ogas_page_camera();
                break;
            case OGAS_GYROSCOPE:
                ogas_page_gyroscope();
                break;
            case FLASH_SET:
                ogas_page_flsah();
                break;
            case OGAS_STATE_GAME:
                ogas_page_game();
                break;
            case OGAS_STATE_DEBUGE:
                ogas_page_debuge();
                break;
            default:
                ogas_page_error();
        }
    }
}

/**
 * @brief 主循环进入不存在页面的提示。
 */
void ogas_page_error(){
    tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
    tft180_show_string(column(0), line(1), "<ERROR>UNDEFINED");
    tft180_show_string(column(0), line(2), "-You have access");
    tft180_show_string(column(0), line(3), "-ed an undefined");
    tft180_show_string(column(0), line(4), "-page, which may");
    tft180_show_string(column(0), line(5), "-cause  an error");
    tft180_show_string(column(0), line(6), "-to occur in the");
    tft180_show_string(column(0), line(7), "-programme.  You");
    tft180_show_string(column(0), line(8), "-will be immedia");
    tft180_show_string(column(0), line(9), "-tely returned .");
    system_delay_ms(1000);
    page_now=OGAS_MASTER;
}

/**
 * @brief 欢迎页面。
 */
void ogas_welcome() {
    tft180_full(RGB565_BLACK);
    
    for (int i = 0; i < 128; i += 4) {
        if(ogas_get_key(0)){
            break;
        }
        tft180_draw_line(i, 0, 127 - i, 159, RGB565_WHITE);
        system_delay_ms(10);

    }
    
    for (int i = 159; i >= 0; i -= 4) {
        if(ogas_get_key(0)){
            break;
        }
        tft180_draw_line(0, i, 127, 159 - i, RGB565_WHITE);
        system_delay_ms(10);

    }
    
    for (int i = 0; i < 15; i++) {
        if(ogas_get_key(0)){
            break;
        }
        int x = (i < 4) ? 112 : ((i < 10) ? 96 : 80);
        int y = 16 * i + ((i < 4) ? 0 : ((i < 10) ? -64 : -160));

        tft180_show_chinese(x, y, 16, WHO[2 * i], 1, RGB565_GRAY);
        system_delay_ms(67);

        tft180_show_chinese(x, y, 16, WHO[2 * i], 1, RGB565_BLACK);
        system_delay_ms(67);
    }
    
    system_delay_ms(500);
}

/**
 * @brief MASTER页面。
 */
void ogas_page_master() {
    tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
    tft180_show_string(column(0), line(1), "<MAETSR>");
    // tft180_show_string(column(0), LINE(2), "");
    tft180_show_string(column(0), line(3), "    START");
    tft180_show_string(column(0), line(4), "    ex.eq. Input");
    tft180_show_string(column(0), line(5), "    FLASH edit");
    tft180_show_string(column(0), line(6), "    ");
    tft180_show_string(column(0), line(7), "    (c)2023");
    // tft180_show_string(column(0), LINE(8), "");
    tft180_show_string(column(0), line(9), "[UP/DOWN/COF/RT]");
    tft180_show_string(column(0), line(3 + (page_target == 3 ? page_target + 1 : page_target)), " ->");

    switch (ogas_get_key(1)) {
        case K1_S:
            page_target = (page_target == 0) ? 3 : page_target - 1;
            break;
        case K2_S:
            page_target = (page_target == 3) ? 0 : page_target + 1;
            break;
        case K3_S:
            switch (page_target) {
                case 0:
                    page_now = OGAS_START;
                    break;
                case 1:
                    page_now = EXEQ;
                    break;
                case 2:
                    page_now = FLASH_SET;
                    break;
            }
            break;
        case K4_S:
            ogas_welcome();
            break;
        default:break;
    }
}

/**
 * @brief 运行模式
 */
void ogas_page_start(){
    state_ac = OGAS_MODE;
    tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
    tft180_show_string(column(0), line(1), "<EXEQ>");
    tft180_show_string(column(0), line(3), "    GAME");
    tft180_show_string(column(0), line(4), "    DEBUG");
    tft180_show_string(column(0), line(9), "[UP/DOWN/COF/RT]");
    tft180_show_string(column(0), line(3) + line(page_target), " ->");

    switch (ogas_get_key(1)) {
        case K1_S:
            page_target = (page_target == 0) ? 1 : page_target - 1;
            break;
        case K2_S:
            page_target = (page_target == 1) ? 0 : page_target + 1;
            break;
        case K3_S:
            switch (page_target) {
                case 0:
                    page_now = OGAS_STATE_GAME;
                    break;
                case 1:
                    page_now = OGAS_STATE_DEBUGE;
            }
            break;
        case K4_S:
            page_now = OGAS_MASTER;
            break;
        default:break;
    }
}
/**
 * @brief 主循环进入比赛模式 最大功率去执行循迹
 */
void ogas_page_game() {
    tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
    tft180_show_string(column(0), line(1), "<EXEQ>");
    tft180_show_string(column(0), line(3), "   GAME MODE");
    tft180_show_string(column(0), line(4), "  is executing");
    tft180_show_string(column(0), line(9), "[UP/DOWN/COF/RT]");
    state_ac = GAME_MODE;
    while (1) {
            //显示赛道信息（处理后的）
            image_process();

            tft180_show_string(column(0) , line(5), "LEFT :");
            tft180_show_int   (column(10), line(5), duty_rl, 3);
            tft180_show_string(column(0) , line(6), "Right:");
            tft180_show_int   (column(10), line(6), duty_rr, 3);
            tft180_show_string(column(0) , line(7), "Side_output:");
            tft180_show_int   (column(10), line(7), side_control_put, 3);
            //tft180_show_string(column(0) , line(7), "Encoder:");
            //tft180_show_int   (column(10), line(7), encoder_data_dir, 3);
            tft180_show_string(column(0) , line(8), "Speed:");
            tft180_show_float (column(10), line(8), increment_out, 3, 1);
            if(wireless_able)
            {
                //Separation_coordinate();
                seekfree_assistant_camera_send();
            }
            switch (ogas_get_key(0)) {
                case K4_S:

                    page_now = OGAS_START;
                    return;
                default:break;
            }
        }
}
/**
 * @brief 调试模式
 */
void ogas_page_debuge(){
    tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
    tft180_show_string(column(0), line(1), "<EXEQ>");
    tft180_show_string(column(0), line(3), "    LEFT  MOTOR");
    tft180_show_string(column(0), line(4), "    RIGHT MOTOR");
    tft180_show_string(column(0), line(5), "    SUSP  MOTOR");
    tft180_show_string(column(0), line(6), "NOW STATE");
    tft180_show_uint  (30,        line(7), state_ac, 3);
    tft180_show_string(column(0), line(8), "[UP/DOWN/COF/RT]");
    tft180_show_string(column(0), line(3) + line(page_target), " ->");

    switch (ogas_get_key(1)) {
    case K1_S:
        page_target = (page_target == 0) ? 2 : page_target - 1;
        break;
    case K2_S:
        page_target = (page_target == 2) ? 0 : page_target + 1;
        break;
    case K3_S:
        switch (page_target) {
        case 0:
            state_ac = DEBUG_MOTOR_BL;
            break;
        case 1:
            state_ac = DEBUG_MOTOR_BR;
            break;
        case 2:
            state_ac = DEBUG_MOTOR_UB;
            break;
        }
        break;
        case K4_S:
            page_now = OGAS_START;
            break;
        default:break;
        }
}
/**
 * @brief EXEQ页面。
 */
void ogas_page_exeq() {
    state_ac = OGAS_MODE;
    tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
    tft180_show_string(column(0), line(1), "<EXEQ>");
    tft180_show_string(column(0), line(3), "    CAMERA");
    tft180_show_string(column(0), line(4), "    GYROSCOPE");
    tft180_show_string(column(0), line(9), "[UP/DOWN/COF/RT]");
    tft180_show_string(column(0), line(3) + line(page_target), " ->");

    switch (ogas_get_key(1)) {
        case K1_S:
            page_target = (page_target == 0) ? 1 : page_target - 1;
            break;
        case K2_S:
            page_target = (page_target == 1) ? 0 : page_target + 1;
            break;
        case K3_S:
            switch (page_target) {
                case 0:
                    page_now = OGAS_CAMREA;
                    break;
                case 1:
                    page_now = OGAS_GYROSCOPE;
            }
            break;
        case K4_S:
            page_now = OGAS_MASTER;
            break;
        default:break;
    }
}
/**
 * @brief 陀螺仪界面。
 */
void ogas_page_gyroscope() {
    int i=0;
    while(1){
        if(i++ == 10000){
    tft180_show_string(0, 0   , "408 Lab. cWUI-V2");
    tft180_show_string(0, 16  , "<GYROSCOPE>");
    tft180_show_string(0, 16*2, "YAW:");
    tft180_show_float (0, 16*3, yaw, 4, 4);
    tft180_show_string(0, 16*4, "ROLL:");
    tft180_show_float (0, 16*5, roll, 4, 4);
    tft180_show_string(0, 16*6, "PITCH:");
    tft180_show_float (0, 16*7, pitch, 4, 4);
    i=0;
        }
        if(ogas_get_key(0) == K4_S) {
            page_now = EXEQ;
            return;
        }
    }
}
/**
 * @brief CAMERA页面。
 */
void ogas_page_camera() {
    state_ac = CAMREA_MODE;
    while (1) {
        //显示赛道信息（处理后的）

        image_process();
        image_display();
        tft180_show_string(0, 0, "VIEW IN SEEKFREE");
        if(seekfree_able){
            Separation_coordinate();
            seekfree_assistant_camera_send();
            sf_ass_o_update();
            sf_ass_p_check();
        }
        
        if(ogas_get_key(0) >= K4_S) {
            flash_write();
            page_now = EXEQ;
            return;
        }
    }
}

/**
 * @brief FLASH页面。
 */
void ogas_page_flsah() {
        tft180_show_string(column(0), line(0), "408 Lab. cWUI-V2");
        tft180_show_string(column(0), line(1)*1, "<FLASH>");
        tft180_show_string(column(0), line(1)*3, "   thres");
        tft180_show_float (column(4), line(4), thres, 4, 2);
        tft180_show_string(column(0), line(1)*5, "   Kd");
        tft180_show_string(column(0), line(1)*9, "[UP/DOWN/COF/RT]");
        switch (symbol)
        {
        case 0:
            tft180_show_string(column(0), line(3) + line(2) * page_target, " ->");
            break;
        case 1:
            tft180_show_string(column(0), line(3) + line(2) * page_target, " **");
            break;
        default:break;
        }

        if(ogas_get_key(1) == K1_S)
        {
            if(NO)
            {
                page_target = (page_target == 0) ? 1 : page_target - 1;
            }
            else if(YES)
            {
                switch(page_target)
                {
                case 0:
                    thres++;
                    break;
                default:break;
                }
            }
        }
        else if(ogas_get_key(1) == K2_S)
        {
            if(NO)
            {
            page_target = (page_target == 1) ? 0 : page_target + 1;
            }
            else if(YES)
            {
                switch(page_target)
                {
                case 0:
                    thres--;
                    break;
                default:break;
                }
            }

        }
        else if(ogas_get_key(1) == K3_S)
        {
            symbol = 1;
        }
        else if(ogas_get_key(1) == K4_S)
        {
            if(NO)
            {
            page_now = OGAS_MASTER;
            flash_read();
            printf("\r\n float_type : %f", flash_union_buffer[0].float_type);
            }
            else if(YES)
            {
                symbol = 0;
                flash_write();
            }
        }
}
